//
//  RankingListTwoViewController.m
//  
//
//  Created by RYX on 2019/1/8.
//

#import "RankingListTwoViewController.h"
#import "RankingListCell.h"
#import "RankingListModel.h"
#import "RankingListStore.h"

@interface RankingListTwoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *parameterDate;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;

@end

@implementation RankingListTwoViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.sectionHeaderHeight = 4.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 1)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf loadData];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        
        [_tableView.mj_header beginRefreshing];
        
    }
    
    return _tableView;
}

- (void)loadData{
    
    MJWeakSelf
    
    RankingListStore *store = [[RankingListStore alloc] init];
    [store getRankingListWithType:@"2" andMonth:parameterDate Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDate *date=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM";
    parameterDate = [dateFormatter stringFromDate:date];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(0);
        
        make.bottom.equalTo(self.view).offset(-45);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    //用户选择了日期
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotify:) name:@"SelectDate" object:nil];
}

- (void)onNotify:(NSNotification *)noti{
    
    NSString *value = noti.userInfo[@"currentDate"];
    
    parameterDate = value;
    
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [RankingListCell getHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RankingListCell *cell = [RankingListCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.unit = @" 台";
    cell.indexPath = indexPath;
    cell.model = self.listArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
